//look into this:
//userSchema.methods.validPassword = function validPass(pass){ validate password code}

var mongoose = require("mongoose");
var mongodb = require("mongodb");

var User = mongoose.model("User");


//accidentally deleted database. used this to recreate admin.
/*exports.runonce = function () {
    User.create({username : "admin",
    password: "admin",
    admin: true});
};*/

exports.saveUser = function (username, password, callback) {


    User.create({
        username: username,
        password: password
    }, function (err, user) {


        if (err) {
            console.log(err);
            callback(false, err);
            return;

        }
        console.log('New user added: ' + user);
        callback(true, "Player added");


    });


};

//online function below will have redundant code duplication. for now. ssssokay brohan, ssssokay.
exports.online = function (username, password, callback) {


    User.find({ "username": username}).where({ "password": password}).exec(function (err, user) {
        if (err) {
            console.log("error performing database query @dbutil.online: " + err);
            return;
        }
        console.log(user + " is attempting to log on");
        if (user.admin === true) {
            console.log("admin");
            res.render("holy shit admin");
        }
        var useronline = {"username": user.username,
            "password": user.password,
            "isadmin": user.admin};

        user.forEach(function (user) {
            useronline.username = user.username;
            useronline.password = user.password;
            useronline.isAdmin = user.admin;


        });
        /*if (useronline.username === username && useronline.password === password) {
            console.log("successful evaluation from dbutil");
        }*/
        console.log(useronline + " logging useronline var from dbutil");


        callback(useronline);
    });


};



exports.lookup = function (callback) {
    User.find({}).exec(function (err, rows) {
        if (err) {
            console.log("error in lookup " + err);
            return;
        }
        var userMap = [];
        rows.forEach(function (rows) {
            userMap.push(JSON.stringify(rows.username));
        });
        console.log("returning data from exports.lookup in dbutil");
        //console.log(rows);

        callback(userMap);


    });
};

exports.removeUser = function (deleteUser) {
    var usr = (deleteUser.replace(/"/g, ""));
    User.remove({username : usr}, function (err) {
        if (err) {
            console.log("error deleting user: " + usr + " with error: " + err);
            return;
        }

        console.log("succesfully deleted: " + usr);
    });
};

exports.updateUser = function (newName, originalName, callback) {
    User.update({username : originalName}, {$set: {username : newName}}, function (err, object) {
        if (err) {
            console.log(err);
            callback(false, err);
        } else {
            console.log("entry successfully updated " + object);
            callback(true, "User Edited");
        }
    });

};


